//
//  FMEditCommentViewController.m
//  PsychologyFM
//
//  Created by zhang on 16/1/14.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMEditCommentViewController.h"

@interface FMEditCommentViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *commentTextView;
@property (nonatomic, strong) UIView *nagevitionBarView;
//评论输入界面的占位
@property (nonatomic, strong) UILabel *lable;
@end

@implementation FMEditCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addTextFeild];
    [self addNageVitionBar];
}
- (void)addTextFeild{

    self.commentTextView = [[UITextView alloc]initWithFrame:CGRectMake(kMartin20, 64, kScreenWidth - kMartin20 * 2, kScreenHeight - 64)];
    self.commentTextView.delegate = self;
    self.commentTextView.font = [UIFont systemFontOfSize:16];
    self.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, kMartin5, 80, 20)];
    _lable.text = @"我想要说";
    _lable.textColor = FMOrange;
    [self.commentTextView addSubview:_lable];
  //  self.commentTextView.text = @"我想要说";
    self.commentTextView.textAlignment = 3;
  //  self.commentTextView.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:_commentTextView];


}
#pragma mark 添加nagevitonBar
- (void)addNageVitionBar{
    self.nagevitionBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 44)];
    //    添加返回按钮
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    cancelButton.frame = CGRectMake(20, 10, 30, 30);
    [cancelButton setImage:[UIImage imageNamed:@"cross1@2x"] forState:(UIControlStateNormal)];
    [cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.nagevitionBarView addSubview:cancelButton];
    [self.view addSubview:_nagevitionBarView];
    UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth - 40) / 2, 0, 40, 40)];
    titleLable.text = @"评论";
    
    [self.nagevitionBarView addSubview:titleLable];
    //添加提交按钮
    UIButton *commiteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    commiteButton.frame = CGRectMake(kScreenWidth - 40, cancelButton.top, 30, cancelButton.height);
    [commiteButton setImage:[UIImage imageNamed:@"tick@2x"] forState:(UIControlStateNormal)];
    [self.nagevitionBarView addSubview:commiteButton];
    
    [commiteButton addTarget:self action:@selector(commitButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 43, kScreenWidth, 1)];
    view.backgroundColor = FMGray;
    [self.nagevitionBarView addSubview:view];
}
//取消评论
- (void)cancelButton:(UIButton *)cancelButton{

    [self dismissViewControllerAnimated:YES completion:nil];

}
// 提交评论
- (void)commitButton:(UIButton *)button{

    
    NSString *str = @"http://yiapi.xinli001.com/fm/post-forum-comment.json";
    
//    flag   key  post_id  content  token
 
     NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    parDic[@"post_id"] = self.post_id;
    
    parDic[@"key"] = @"c0d28ec0954084b4426223366293d190";
    parDic[@"token"] = self.token;
    NSLog(@"%@",_token);
    parDic[@"flag"] = @"0";
   // parDic[@"reply_user_id"] = self.reply_user_id;
    NSLog(@"%@",self.reply_user_id);
    
    parDic[@"content"] = self.commentTextView.text;
    
    [PKRequestManager requestWithUrlString:str parDic:parDic method:POST finish:^(NSData *data) {
       
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSLog(@"%@",dict[@"massage"]);
        NSDictionary *dic = dict[@"data"];
            
            if ([_delegate respondsToSelector:@selector(passData:)] && _delegate != nil) {
                [_delegate passData:data];
            }
            
            
     
        
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    [self dismissViewControllerAnimated:YES completion:nil];


}
//UITextView 遵守的协议
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    //[self.lable removeFromSuperview];
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.lable.hidden = NO;
    }else{
        self.lable.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
